package by.grsu.perovskij.tools;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import by.grsu.perovskij.dao.BillDao;
import by.grsu.perovskij.dao.PassengerDao;
import by.grsu.perovskij.dao.PassengerWagonDao;
import by.grsu.perovskij.dao.ScheduleRecordDao;
import by.grsu.perovskij.dao.TicketRequestDao;
import by.grsu.perovskij.dao.TrainDao;
import by.grsu.perovskij.dao.WagonPlaceDao;
import by.grsu.perovskij.entity.Bill;
import by.grsu.perovskij.entity.Passenger;
import by.grsu.perovskij.entity.PassengerWagon;
import by.grsu.perovskij.entity.ScheduleRecord;
import by.grsu.perovskij.entity.TicketRequest;
import by.grsu.perovskij.entity.Train;
import by.grsu.perovskij.entity.WagonComfort;
import by.grsu.perovskij.entity.WagonPlace;

import com.epam.grsulab.webapi.dao.base.Dao;
import com.epam.grsulab.webapi.entity.base.BaseEntity;
import com.epam.grsulab.webapi.tools.DataInitialiser;

@Component
public class DataInitialiserImpl implements DataInitialiser {

	private static final Logger logger = LoggerFactory
			.getLogger(DataInitialiserImpl.class);

	@Autowired
	private BillDao billDao;
	@Autowired
	private TrainDao trainDao;
	@Autowired
	private ScheduleRecordDao scheduleRecordDao;
	@Autowired
	private PassengerDao passengerDao;
	@Autowired
	private TicketRequestDao ticketRequestDao;
	@Autowired
	private PassengerWagonDao wagonDao;
	@Autowired
	private WagonPlaceDao wagonPlaceDao;

	@Override
	public void initData() {
		Train train1 = new Train();
		Train train2 = new Train();
		train1 = trainDao.save(train1);
		train2 = trainDao.save(train2);

		// for train1
		PassengerWagon wagon11 = new PassengerWagon();
		fillWagon(train1, wagon11, WagonComfort.business);
		wagon11 = wagonDao.save(wagon11);
		PassengerWagon wagon12 = new PassengerWagon();
		fillWagon(train1, wagon12, WagonComfort.middle);
		wagon12 = wagonDao.save(wagon12);
		// for train2
		PassengerWagon wagon21 = new PassengerWagon();
		fillWagon(train2, wagon21, WagonComfort.middle);
		wagon21 = wagonDao.save(wagon21);
		PassengerWagon wagon22 = new PassengerWagon();
		fillWagon(train2, wagon22, WagonComfort.cheap);
		wagon22 = wagonDao.save(wagon22);

		ArrayList<PassengerWagon> wagons = new ArrayList<PassengerWagon>();
		wagons.add(wagon11);
		wagons.add(wagon12);
		wagons.add(wagon21);
		wagons.add(wagon22);
		
		SetWagonPlaces(wagons);

		ScheduleRecord scheduleRecord1 = new ScheduleRecord();
		fillScheduleRecord(train1, scheduleRecord1, "station1", "station2");
		scheduleRecord1 = scheduleRecordDao.save(scheduleRecord1);
		ScheduleRecord scheduleRecord2 = new ScheduleRecord();
		fillScheduleRecord(train2, scheduleRecord2, "station3", "station4");
		scheduleRecord2 = scheduleRecordDao.save(scheduleRecord2);

		ArrayList<Passenger> passengers = new ArrayList<Passenger>();
		passengers.add(createPassenger("Alexej", "Perovskij"));
		passengers.add(createPassenger("Dimon", "Dimych"));
		passengers.add(createPassenger("Roman", "Romskij"));
		passengers.add(createPassenger("Sanja", "Sanjinski"));

		ArrayList<TicketRequest> ticketRequests = new ArrayList<TicketRequest>();
		ticketRequests.add(createTicketRequest(null,scheduleRecord1,passengers.get(0)));
		ticketRequests.add(createTicketRequest(null,scheduleRecord1,passengers.get(1)));
		ticketRequests.add(createTicketRequest(null,scheduleRecord2,passengers.get(2)));
		ticketRequests.add(createTicketRequest(null,scheduleRecord2,passengers.get(3)));
		
		ArrayList<Bill> bills = new ArrayList<Bill>();
		bills.add(createBill(ticketRequests.get(0)));
		bills.add(createBill(ticketRequests.get(1)));
		bills.add(createBill(ticketRequests.get(2)));
		bills.add(createBill(ticketRequests.get(3)));

		

	}

	private TicketRequest createTicketRequest(Bill bill, ScheduleRecord record,Passenger p) {
		TicketRequest ticketRequest = new TicketRequest();
		ticketRequest.setScheduleRecord(record);
		ticketRequest.setBill(bill);
		ticketRequest.setPassenger(p);
		ticketRequest = ticketRequestDao.save(ticketRequest);
		return ticketRequest;
	}

	private Bill createBill(TicketRequest request) {
		Bill bill = new Bill();
		bill.setCost(2.1);
		bill.setPassenger(request.getPassenger());
		bill.setScheduleRecord(request.getScheduleRecord());
		bill = billDao.save(bill);
		return bill;
	}

	private Passenger createPassenger(String firstName, String secondName) {
		Passenger passenger = new Passenger();
		passenger.setFirstName(firstName);
		passenger.setSecondName(secondName);
		passenger = passengerDao.save(passenger);
		return passenger;
	}

	private void fillScheduleRecord(Train train, ScheduleRecord scheduleRecord,
			String station1, String station2) {
		scheduleRecord.setArrivalStation(station1);
		scheduleRecord.setDepartureStation(station2);
		scheduleRecord.setArrivalDate(Calendar.getInstance());
		scheduleRecord.setDepartureDate(Calendar.getInstance());
		scheduleRecord.setName(station1 + " " + station2);
		scheduleRecord.setTrain(train);
	}

	private void SetWagonPlaces(ArrayList<PassengerWagon> wagons) {

		for (PassengerWagon wagon : wagons) {
			for (int i = 0; i < wagon.getCapacity(); i++) {
				WagonPlace wagonPlace = new WagonPlace();
				wagonPlace.setOccupied(false);
				wagonPlace.setWagon(wagon);
				wagonPlace = wagonPlaceDao.save(wagonPlace);
			}
		}

	}

	private void fillWagon(Train train, PassengerWagon wagon,
			WagonComfort comfort) {
		wagon.setCapacity(20);
		wagon.setComfort(comfort);
		wagon.setQuantity(0);
		wagon.setTrain(train);
	}

	@Override
	public List<Dao<? extends BaseEntity>> getAllDAOs() {
		List<Dao<? extends BaseEntity>> list = new ArrayList<Dao<? extends BaseEntity>>();

		list.add(trainDao);
		list.add(wagonDao);
		list.add(wagonPlaceDao);
		list.add(scheduleRecordDao);
		list.add(passengerDao);
		list.add(billDao);
		list.add(ticketRequestDao);

		return list;
	}

}
